/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.service.baseData;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.chenweihuacwh.dao.baseData.CodeDAO;
import com.javaeye.chenweihuacwh.dto.baseData.Code;
import com.javaeye.chenweihuacwh.dao.orderManage.PurchaseOrderDetailDAO;

public class CodeService implements ICodeService {
	
	private static Log log = LogFactory.getLog(CodeService.class);
	
	private CodeDAO dao;
	
	private PurchaseOrderDetailDAO orderDetailDao;
	
	public void setDao(CodeDAO dao) {
		this.dao = dao;
	}
	
	public void setOrderDetailDao(PurchaseOrderDetailDAO orderDetailDao) {
		this.orderDetailDao = orderDetailDao;
	}

	@Override
	public Code getCode(String id) {
		int intId = Integer.parseInt(id);
		Code code = dao.getCode(intId);
		if (code == null) {
			log.warn("在数据库中未找到指定的代码  id：" + id);
		}
		return code;
	}

	@Override
	public String getCodeName(String id) {
		Code code = getCode(id);
		if (code == null) {
			return "";
		}
		return code.getName();
	}

	@Override
	public List<Code> getCodesByCategoryId(String categoryId) {
		List<Code> result = dao.getCodesByCategoryId(categoryId);
		if (result == null || result.size() == 0) {
			log.warn("在数据库中未找到代码数据 categoryId: " + categoryId);
		}
		return result;
	}

	@Override
	public boolean removeCode(String id) {
		int intId = Integer.parseInt(id);
		long count = orderDetailDao.getCountByUnitId(intId);
		if (count > 0) {
			return false;
		}
		dao.removeCode(intId);
		return true;
	}

	@Override
	public Code saveCode(Code code) {
		dao.saveCode(code);
		return code;
	}

}
